package com.ncloud.resman.mapper;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.DeviceBase;

import java.util.List;

import org.apache.ibatis.annotations.*;

/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface DeviceBaseMapper {
    @Insert("insert into res_device_base(device_id, sn_num, model_id, model_name, brand_id, brand_name, eqpt_type, type_name," +
            " eqpt_spec, spec_name, state,create_user,create_date,update_user,update_date)"
            + " values(#{deviceId},#{snNum},#{modelId},#{modelName},#{brandId},#{brandName},#{eqptType},#{typeName}," +
            "#{eqptSpec},#{specName},#{state},#{createUser},#{createDate},#{updateUser},#{updateDate})")
    int insertDeviceBase(DeviceBase deviceBase);
    @Update("update res_device_base set sn_num = #{snNum},model_id=#{modelId},model_name =#{modelName},brand_id =#{brandId}," +
            "brand_name =#{brandName},eqpt_type =#{eqptType},type_name=#{typeName},eqpt_spec=#{eqptSpec},spec_name=#{specName}," +
            "state=#{state},update_user=#{updateUser},update_date=#{updateDate} where device_id =#{deviceId}")
    int updateDeviceBase(DeviceBase deviceBase);
	@Update("update res_device_base set state=#{1} where device_id =#{0}")
	int updateDeviceBaseState(Long deviceId,String state);
    @Select("SELECT CONCAT(device_id,'') as device_id, sn_num, CONCAT(model_id,'') as model_id, model_name, CONCAT(brand_id,'') as brand_id, brand_name, eqpt_type, "
    		+ "type_name, eqpt_spec, spec_name, state "
			+ "FROM res_device_base where device_id = #{deviceId}")
	@Results({
		@Result(column="device_id",property="deviceId"),@Result(column="sn_num",property="snNum"),
		@Result(column="model_id",property="modelId"),@Result(column="model_name",property="modelName"),
		@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
		@Result(column="eqpt_type",property="eqptType"),@Result(column="type_name",property="typeName"),
		@Result(column="eqpt_spec",property="eqptSpec"),@Result(column="spec_name",property="specName"),
		@Result(column="state",property="state")})
	List<JSONObject> getDeviceBaseById(Long deviceId);//根据设备id查询设备基本信息
	@Select("SELECT * FROM res_device_base where device_id = #{0}")
	@Results({
			@Result(column="device_id",property="deviceId"),@Result(column="sn_num",property="snNum"),
			@Result(column="model_id",property="modelId"),@Result(column="model_name",property="modelName"),
			@Result(column="brand_id",property="brandId"),@Result(column="brand_name",property="brandName"),
			@Result(column="eqpt_type",property="eqptType"),@Result(column="type_name",property="typeName"),
			@Result(column="eqpt_spec",property="eqptSpec"),@Result(column="spec_name",property="specName"),
			@Result(column="state",property="state"),@Result(column="create_user",property="createUser"),
			@Result(column="create_date",property="createDate"),@Result(column="update_user",property="updateUser"),
			@Result(column="update_date",property="updateDate")})
	DeviceBase getDeviceBaseByDeviceId(Long deviceId);

	/*删除*/
	@Delete(value = "delete from res_device_base WHERE device_id = #{0} and state='3'")
	int deleteDeviceBase(Long deviceId);
	
	@Select(value = {"select count(1) from res_device_base where sn_num = #{0}"})
	int existsDeviceBySnNum(String snNum);
	
	@Select(value = {"select device_id from res_device_base where sn_num = #{0}"})
	Long getDeviceIdBySnNum(String snNum);
	
	
}
